package com.gzhu.knowledgeAdmin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzhu.knowledgeAdmin.common.constant.OperatorType;
import com.gzhu.knowledgeAdmin.common.vo.OperatorVo;
import com.gzhu.knowledgeAdmin.common.vo.PageVo;
import com.gzhu.knowledgeAdmin.entity.Keyword;
import com.gzhu.knowledgeAdmin.service.KeywordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.gzhu.knowledgeAdmin.dao.OperatorDao;
import com.gzhu.knowledgeAdmin.entity.Operator;
import com.gzhu.knowledgeAdmin.service.OperatorService;


@Service("operatorService")
public class OperatorServiceImpl extends ServiceImpl<OperatorDao, Operator> implements OperatorService {

    @Autowired
    private KeywordService keywordService;

    @Override
    public PageVo queryPage(Map<String, Object> params) {
        // 获取当前页和每页大小
        long pageNo = Long.parseLong((String) params.get("page"));
        long pageSize = Long.parseLong((String) params.get("limit"));

        Page<Operator> page = new Page<>(pageNo,pageSize);
        QueryWrapper<Operator> wrapper = new QueryWrapper<>();
        // TODO 根据params里的查询参数添加条件查询

        baseMapper.selectPage(page,wrapper);

        return new PageVo(page);
    }

    @Override
    public Operator saveOrUpdateOperator(OperatorVo operatorVo) {
        // 如果为 包含关键词类型
        if(operatorVo.getType()== OperatorType.CONTAIN_KEYWORD){

            // 拼接所有关键词的id
            StringBuilder stringBuilder = new StringBuilder();
            List<Integer> selectedKeywords = operatorVo.getSelectedKeywords();
            for (Integer item : selectedKeywords) {
                stringBuilder.append(item.toString()).append(',');
            }
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
            operatorVo.setContent(stringBuilder.toString());

            Operator operator = new Operator();
            BeanUtils.copyProperties(operatorVo,operator);

            saveOrUpdate(operator);

            // 设置替换关键词id,设置到内容中
            StringBuilder contentBuilder = new StringBuilder();
            Collection<Keyword> keywords = keywordService.listByIds(selectedKeywords);
            for (Keyword keyword : keywords) {
                contentBuilder.append(keyword.getName()).append('|');
            }
            contentBuilder.deleteCharAt(contentBuilder.length()-1);

            operator.setContent(contentBuilder.toString());
            return operator;

        }else {
            Operator operator = new Operator();
            BeanUtils.copyProperties(operatorVo,operator);

            saveOrUpdate(operator);

            return operator;
        }



    }

    @Override
    public OperatorVo getOperatorById(Integer id) {

        Operator operator = getById(id);

        // 复制属性
        OperatorVo operatorVo = new OperatorVo();
        BeanUtils.copyProperties(operator,operatorVo);

        // 如果是 包含关键词类型 ，设置selectedKeywords
        if (operator.getType()==OperatorType.CONTAIN_KEYWORD){
            String content = operator.getContent();
            String[] split = content.split(",");
            ArrayList<Integer> keywordIds = new ArrayList<>();
            for (String s : split) {
                keywordIds.add(Integer.parseInt(s));
            }
            operatorVo.setSelectedKeywords(keywordIds);

            // 设置content为空
            operatorVo.setContent(null);
        }

        return operatorVo;

    }



}